package com.yc.upload;

import com.yc.upload.bean.UploadProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@ComponentScan("com.yc.upload")
@ConditionalOnProperty(prefix = "upload", name = "enable")
public class UploadAutoConfiguration implements WebMvcConfigurer {

    @Resource
    UploadProperties properties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        properties.getMappings().forEach( i -> {
            String webPath = "/" + i.getWebPath() + "/**";
            String disPath = "file:" + i.getDiskPath();
            registry.addResourceHandler(webPath)
                    .addResourceLocations(disPath);
        });
    }

}
